package com.zwz.controller;

import com.zwz.crawler.basic.TungeeAlterationQuery;
import com.zwz.crawler.basic.TungeeEnterpriseInfo;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.InformationChange;
import com.zwz.service.InformationChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/info-changes")
public class InformationChangeController {

    @Autowired
    private InformationChangeService service;

    @PostMapping
    //public ResponseEntity<?> create(@RequestBody InformationChange record) {
    public ResponseEntity<?> create( String enterpriseId ) {

        try {
           //不加了
            return ResponseEntity.status(HttpStatus.CREATED).body(null);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<InformationChange> getById(@PathVariable Integer id) {
        InformationChange record = service.getById(id);
        return record != null ? ResponseEntity.ok(record) : ResponseEntity.notFound().build();
    }

    @GetMapping
    public ResponseEntity<List<InformationChange>> getAll() {
        return ResponseEntity.ok(service.getAll());
    }

    @GetMapping("/type/{changeType}")
    public ResponseEntity<List<InformationChange>> getByType(@PathVariable String changeType) {
        try {
            return ResponseEntity.ok(service.getByChangeType(changeType));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable Integer id, @RequestBody InformationChange record) {
        try {
            if (!id.equals(record.getId())) {
                return ResponseEntity.badRequest().body("ID不一致");
            }
            service.update(record);
            return ResponseEntity.ok(record);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteById(@PathVariable Integer id) {
        service.deleteById(id);
        return ResponseEntity.noContent().build();
    }
}